package com.polaris.live.ui.vip.activity

import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.StringUtils
import com.polaris.live.common.adapter.ViewPagerAdapter
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.AppConst
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.network.parseState
import com.polaris.live.common.util.AppCodeUtils
import com.polaris.live.common.util.ImmersionBarBuilder
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.ActivityVipCenterBinding
import com.polaris.live.manager.AppConfigManager
import com.polaris.live.translation.R
import com.polaris.live.ui.vip.fragment.VipCenterMainFragment
import com.polaris.live.ui.vip.fragment.VipCenterOwnMainFragment
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.route.RouterUtils
import com.polaris.live.utils.getIntOrString
import com.polaris.live.viewmodel.VipCenterViewModel
import com.polaris.live.widget.tab.TabBean

/**
 * VipCenterActivity vip中心
 * @author Created by 天晴 on 2024/12/10 10:02
 * @since 1.0.0
 **/
class VipCenterActivity : BaseVbActivity<VipCenterViewModel, ActivityVipCenterBinding>() {

    private val mFragments = ArrayList<Fragment>()
    private val mTitles = ArrayList<TabBean>()
    private var mTabAdapter: ViewPagerAdapter? = null

    private var mCurrentItemIndex = 0

    private val mPage by lazy { intent.getIntOrString("page", -1) }

    private val mLevel by lazy { intent.getIntOrString("level", -1) }

    private val mVipCenterMainFragment by lazy {
        VipCenterMainFragment().apply {
            arguments = bundleOf("level" to mLevel)
            upData {
                this@VipCenterActivity.mViewModel.getVipCenter()
            }
            toVip(::navigateToVipCenterOwn)
        }
    }
    private val mVipCenterOwnMainFragment by lazy {
        VipCenterOwnMainFragment().apply {
            arguments = bundleOf("level" to mLevel)
            upData {
                this@VipCenterActivity.mViewModel.getVipCenter()
            }
            toVip(::navigateToVipCenter)
        }
    }

    private fun navigateToVipCenterOwn(level: Int) {
        mCurrentItemIndex = 1
        toIndex(mCurrentItemIndex, true)
        mVipCenterOwnMainFragment.goPageSelected(level)
    }

    private fun navigateToVipCenter(level: Int) {
        mCurrentItemIndex = 0
        toIndex(mCurrentItemIndex, true)
        mVipCenterMainFragment.goPageSelected(level)
    }

    override fun ImmersionBarBuilder.buildImmersionBar() {
        setTitleBar(mBinding.titleRl)
        setStatusBarDarkFont(false)
        setNavigationBarDarkFont(false)
        attachImmersionBar { transparentBar() }
    }

    override fun initView(savedInstanceState: Bundle?) {
        if (mPage != -1) {
            mCurrentItemIndex = mPage
        } else {
            val vipLevel = UserManager.getUser().profile?.vipLevel ?: 0
            if (vipLevel > 0L) {
                mCurrentItemIndex = 1
            }
        }

        mFragments.add(mVipCenterMainFragment)
        mTitles.add(TabBean(StringUtils.getString(R.string.vip_center)))
        mFragments.add(mVipCenterOwnMainFragment)
        mTitles.add(TabBean(StringUtils.getString(R.string.my_vip)))
        mTabAdapter = ViewPagerAdapter(mFragments, this)
        mBinding.viewpager2.adapter = mTabAdapter
        mBinding.viewpager2.offscreenPageLimit = mFragments.size
        mBinding.tab.setViewPage(mBinding.viewpager2)
        mBinding.tab.setTabs(mTitles)
        toIndex(mCurrentItemIndex, false)

        initListener()
        mViewModel.getVipCenter()
        if (AppCodeUtils.isAR()) {
            mBinding.closeImg.scaleX = -1f
        }
    }

    private fun toIndex(index: Int, smoothScroll: Boolean) {
        if (index in mFragments.indices) {
            mCurrentItemIndex = index
            mBinding.viewpager2.setCurrentItem(mCurrentItemIndex, smoothScroll)
            mBinding.tab.onPageSelected(mCurrentItemIndex)
        }
    }

    private fun initListener() {
        mBinding.customerImg.setOnClickListener {
            val safeUrl = AppConfigManager.getConfig(CommonConst.Configs.customerServiceUrl)
            RouterUtils.routerWeb(ImageUtils.imgPath(safeUrl))
        }
        mBinding.whatImg.setOnClickListener {
            RouterUtils.routerWeb(ImageUtils.imgPath(AppConst.VipDescription.suffix))
        }
        mBinding.closeImg.setOnClickListener {
            finish()
        }

        mViewModel.vipCenterRespLiveData.observe(this) { resultState ->
            parseState(resultState, {
                mVipCenterMainFragment.setData(it)
                mVipCenterOwnMainFragment.setData(it)
            }, {
                ToastGravity.showGravity(it.errorMsg)
            })
        }
    }

}